package com.springpad;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

import com.database.NoteAdapter;
import com.model.Note;

import firstdroid.tutorial.gridview.R;
public class Menu extends Activity {
	AlertDialog.Builder alert;
	private Context ctx;
	NoteAdapter na;
	String nid;
	int i;
	ArrayList<Note> groupsList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		na = new NoteAdapter(getApplicationContext());
		ctx = this;
		nid=getIntent().getExtras().getString("nbid");
		ImageButton b=(ImageButton) findViewById(R.id.imageButton3);
		 b.setOnClickListener(new OnClickListener() {
			 
				@Override
				public void onClick(View v) {
					startActivity(new Intent(Menu.this,MyGridView.class));
					
				}
			});
		 ImageButton view=(ImageButton) findViewById(R.id.imageButton4);
		 view.setOnClickListener(new OnClickListener() {
			 
				@Override
				public void onClick(View v) {
					Intent i=new Intent(Menu.this,ViewNote.class);
					i.putExtra("nbid",nid);
					startActivity(i);
					//startActivity(new Intent(Menu.this,ViewNote.class));
					
				}
			});
		 
		 
		 
		 
		 ImageButton search=(ImageButton) findViewById(R.id.imageButton2);
		 search.setOnClickListener(new OnClickListener() {
			 
				@Override
				public void onClick(View v) {
					alert=new AlertDialog.Builder(ctx);
					alert.setTitle("Search Note");
			    	alert.setMessage("Enter Note name ");
			    	final EditText name = new EditText(ctx);
					alert.setView(name);
			    	alert.setPositiveButton("Search", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							
							i=searchNote(name.getText().toString());
							if(i==0)
							{
								alert=new AlertDialog.Builder(ctx);
								alert.setTitle("Result");
						    	alert.setMessage("No note found ");
						    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog, int which) {
										
										
										
									}
								});
						    	
						    	alert.create().show();
							}
							else
							{
								alert=new AlertDialog.Builder(ctx);
								alert.setTitle("Result");
						    	alert.setMessage(groupsList.get(1).getName());
						    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog, int which) {
										
										
										
									}
								});
						    	
						    	alert.create().show();
							}
							//startActivity(new Intent(MyGridView.this,						MyGridView.class));
							
						}
					});
			    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	
						@Override
						public void onClick(DialogInterface dialog, int which) {
							
						}
					});
			    	alert.create().show();
					
				}
			});
		 
		 
		 
		 ImageButton addnote=(ImageButton) findViewById(R.id.imageButton1);
		 addnote.setOnClickListener(new OnClickListener() {
			 
				@Override
				public void onClick(View v) {
					Intent i=new Intent(Menu.this,AddNote.class);
					i.putExtra("nbid",nid);
					startActivity(i);
					//startActivity(new Intent(Menu.this,AddNote.class));
					
				}
			});
		 
		 
	}
	
	private int searchNote(String name) {
		na.open();
		groupsList = na.fetchNoteOnName(name);
		na.close();
		if(groupsList.size()>0)
		{
			return 1;
		}
		 Log.d("count----->",groupsList.size()+"");
		return 0;
		
	}
}